<script>
function beautify_select(obj){
	var s = $(obj);
	var selects = s.children('select');
	var zindex = selects.attr('z-index') || s.attr('z-index') || 99;
	var width = (selects.attr('width') || '').replace('px', '');
	
	var disabled = selects.is(':disabled');
	var items = selects.find('option');
	var val = selects.attr('data-value') || selects.val();
	selects.val(val+''.split(','));
	var selected = selects.find('option:selected');
	var text = selected.text();
	var li = [];
	items.each(function(){
		var text = $(this).text();
		var val = $(this).val();
		li.push('<li data-value="'+val+'"><a href="javascript:;">'+text+'</a></li>');
	});
	var div = '<dl class="select-options"><dt style="width:'+(width ? (width-10)+'px' : '')+'">'+text+'</dt><dd><ul style="width:'+(width ? width+'px' : '')+'">'+li.join('')+'</ul></dd></dl>';
	s.find('dl').remove();
	s.append(div);
	
	var z=parseInt(s.css("z-index"));
	var dl = s.find("dl").css('z-index', zindex);
	var dt = s.find("dt");
	var dd = s.find("dd");
/*		dt.bind('click', function(){
		dd.slideDown(200);
		dt.addClass("cur");
		dl.css("z-index",z+1);
	});*/
	if (!disabled){
		dl.toggle(function(){
			dd.slideDown(200);
			dt.addClass("cur");
			dl.css("z-index",z+1);
		},function(){
			dd.slideUp(200);
			dt.removeClass("cur");
			dl.css("z-index",z);
		});
		
		dd.find('li').bind('click', function(){
			$(this).addClass('selected').siblings().removeClass('selected');
			s.children('select').val($(this).attr('data-value'));
			selects.change();
			dt.html($(this).html());
			dd.slideUp(200);
			dt.removeClass("cur");
			dl.css("z-index",z);
		});
	}
	
}
$(function(){
    var url = document.URL ;
	var request = window.location.pathname;
	//alert()
	if (window == top){ 
        var urls = "<?php echo \Yii::$app->urlManager->createUrl('index/');?>?main="+request;
        //top.location.href = "<?php echo \Yii::$app->urlManager->createUrl('index/');?>";	
		
	}
	//从mian里跳转的页面，修复左栏菜单高亮
	$("#admin-menu ul:visible a", parent.document).each(function(i, v){
		var href = $(this).attr('href').toLowerCase();
		if (href == request.toLowerCase()){
			$(this).parent().addClass('sel').siblings().removeClass('sel');
		}
	});

	
	

	
	//删除确认框 防止误删除
	$('.operation-delete').click(function(){
		var url = $(this).attr('href');
		var tips = $(this).attr('title');
		Xconfirm(tips || '确定要删除吗', function(){
			href(url);
		},
		function(){ }
		);
		return false;
	});
	//下拉框select美化
	$('.ch-select').each(function(){
		beautify_select($(this));
	});
	
	$('.btn-select').each(function(){
		var option = '';
		var selects = $(this);
		var selectitem = selects.children('select');
        var val = selectitem.attr('data-value') || selectitem.val();
		selectitem.val(val+''.split(','));
		
		var isselect =  $(this).children('select').length>0;
		var multiple = $(this).children('select').attr('multiple');
		var items = isselect ? $(this).children('select').find('option') : $(this).children('input[type=radio]');
		items.each(function(){
			var text = isselect ? $(this).text() : $(this).attr('title');
			var disabled = $(this).attr('disabled') == 'disabled' ? ' disabled' : '';
			selects.append('<label class="cb-center'+disabled+'"><span>'+text+'</span></label>')
		});
		selects.children('label:first').removeClass('cb-center').addClass('cb-first');
		selects.children('label:last').removeClass('cb-center').addClass('cb-last');
		//alert(num)
		
		selects.find('input,option').each(function(){
			if ($(this).is(":selected") || $(this).is(":checked")){
			    var eq = selects.find('input,option').index($(this));
			    selects.children('label').eq(eq).addClass('selected');
			}
			
		});
		//var v = $(this).find('input:checked,option:selected').index();
		
		//$(this).children('label').eq(v).addClass('selected').siblings().removeClass('selected');
		    /*checkbox radio切换.siblings(*/
		
		selects.children('label:not(.disabled)').click(function(){
			if (selectitem.attr('disabled') == 'disabled'){
				art.dialog.tips('<font color=red>不允许编辑</font>', 2);
				return false;
			}
			var index = selects.find('label').index(this);
			var parent = $(this).parents('.btn-select');
			if (multiple == 'multiple'){
				var sel = $(this).hasClass('selected');
				sel ? $(this).removeClass('selected') : $(this).addClass('selected');
				$('select option',parent).eq(index).attr('selected', !sel);
				
			}else{
				$(this).addClass('selected').siblings().removeClass('selected');
				
				if (isselect){
					
					$('select option',parent).eq(index).attr('selected', true).siblings().attr('selected',false);
				}else{
					$('input',parent).eq(index).attr('checked', true);
				}
			}
		});
		
	});
	
	$('.btn-checkbox').bind('click', function(){
		var checkbox = $(this).children('input');
		var ischecked = checkbox.is(':checked');
		//checkbox.attr('checked', !ischecked);
		//alert(ischecked);
		!ischecked ? $(this).removeClass('checked') : $(this).addClass('checked');
		//checkbox.click();
	});
	
	$('.btn-checkbox').each(function(){
		var checkbox = $(this).children('input:checkbox');
		var ischecked = checkbox.is(':checked');
		//checkbox.attr('checked', !ischecked);
		//alert(ischecked);
		ischecked ? $(this).addClass('checked') : $(this).removeClass('checked');
	});
	


});
</script>